package com.lw.service.impl;

import com.lw.common.utils.StrUtils;
import com.lw.service.TbDictItemService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.lw.entity.TbDict;
import com.lw.mapper.TbDictMapper;
import com.lw.service.TbDictService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import static com.lw.entity.table.TbDictItemTableDef.TB_DICT_ITEM;
import static com.lw.entity.table.TbDictTableDef.TB_DICT;

/**
 * 字典表 服务层实现。
 *
 * @author lw
 * @since 2025年05月17日 07:58:04
 */
@Service
public class TbDictServiceImpl extends ServiceImpl<TbDictMapper, TbDict>  implements TbDictService{

    @Resource
    private TbDictItemService tbDictItemService;
    /**
     * 分页条件查询
     * 经典案例 : 或者查询 或查询 或条件查询
     * @param dict 字典表
     * @return 分页对象
     */
    @Override
    public Page<TbDict> getDictPage(TbDict dict) {
        long pageNumber = dict.getPageNum();
        long pageSize = dict.getPageSize();
        Page<TbDict> page = new Page<>(pageNumber, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        String keywords = dict.getKeywords();
        if (StrUtils.hasLength(keywords)) {
            keywords = keywords.trim();
            queryWrapper.where(TB_DICT.NAME.like(keywords).or(TB_DICT.DICT_CODE.like(keywords)));
        }
        queryWrapper.orderBy(TB_DICT.UPDATE_TIME.getName());
        return page(page,queryWrapper);
    }

    @Override
    public Boolean saveDict(TbDict tbDict) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TB_DICT.DICT_CODE.getName(), tbDict.getDictCode());
        queryWrapper.eq(TB_DICT.NAME.getName(), tbDict.getName());
        long count = count(queryWrapper);
        if (count > 0){
            throw new RuntimeException("字典码或字典名称已存在");
        }
        return save(tbDict);

    }

    @Override
    public Boolean updateDict(TbDict tbDict) {
        TbDict dict = getById(tbDict.getId());
        if (dict == null){
            throw new RuntimeException("字典不存在");
        }
        //检验字典码唯一
        String dictCode = tbDict.getDictCode();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TB_DICT.DICT_CODE.getName(), dictCode);
        queryWrapper.ne(TB_DICT.ID.getName(), tbDict.getId());
        long count = count(queryWrapper);
        if (count > 0){
            throw new RuntimeException("字典码已存在");
        }
        return updateById(tbDict);
    }

    @Override
    public Boolean deleteDict(Long id) {
        TbDict dict = getById(id);
        if (dict == null){
            throw new RuntimeException("字典不存在");
        }
        boolean b = this.removeById(id);
        //删除字典项
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TB_DICT_ITEM.DICT_CODE.getName(), dict.getDictCode());
        boolean remove = tbDictItemService.remove(queryWrapper);
        return b && remove;
    }

}
